﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media;
using System.Reflection;

namespace GenTheme.Theme
{
    internal class ThemeObject : IThemeObject
    {
        private int StepsCount;
        private int[] Steps;
        private IThread topThread;
        private IThread botThread;

        private IRow Owner;

        public ThemeObject(Color? tColor, IRow owner) : this(tColor, owner, typeof(Thread))
        {
        }

        public ThemeObject(Color? tColor, IRow owner, Type type)
        {
            this.Owner = owner;

            ConstructorInfo ci = type.GetConstructor(new Type[] { typeof(Color?), typeof(IThemeObject) });
            this.topThread = (IThread)ci.Invoke(new object[2] { tColor, this });
            this.botThread = (IThread)ci.Invoke(new object[2] { null, this });
        }

        #region IThemeObject Members

        public int ObjectId
        {
            get
            {
                return this.Row.IndexOf(this);
            }
        }

        public IRow Row
        {
            get
            {
                return this.Owner;
            }
        }

        public IThemeObject RightTop
        {
            get
            {
                if (this.Row.RowId > 0 || (this.Row.IsLongRow && this.Row.RowId - 1 > 0))
                {
                    if (this.Row.IsLongRow)
                    {
                        if (this.ObjectId == this.Row.Count - 1)
                        { 
                            return this.Row.Theme[this.Row.RowId - 2][this.ObjectId];
                        }
                        return this.Row.Theme[this.Row.RowId - 1][this.ObjectId];
                    }
                    else
                    { 
                        return this.Row.Theme[this.Row.RowId - 1][this.ObjectId + 1];
                    }
                }
                return null;
            }
        }

        public IThemeObject LeftTop
        {
            get
            {
                if (this.Row.RowId > 0 || (this.Row.IsLongRow && this.Row.RowId - 1 > 0))
                {
                    if (this.Row.IsLongRow)
                    {
                        if (this.ObjectId == 0)
                        { 
                            return this.Row.Theme[this.Row.RowId - 2][this.ObjectId];
                        }
                        return this.Row.Theme[this.Row.RowId - 1][this.ObjectId - 1];
                    }
                    else
                    { 
                        return this.Row.Theme[this.Row.RowId - 1][this.ObjectId];
                    }
                }
                return null;
            }
        }

        public IThemeObject RightBot
        {
            get
            {
                if (this.Row.RowId < this.Row.Theme.Count - 1 || (this.Row.IsLongRow && this.Row.RowId + 1 < this.Row.Theme.Count - 1))
                {
                    if (this.Row.IsLongRow)
                    {
                        if (this.ObjectId == this.Row.Count - 1)
                        { 
                            return this.Row.Theme[this.Row.RowId + 2][this.ObjectId];
                        }
                        return this.Row.Theme[this.Row.RowId + 1][this.ObjectId];
                    }
                    else
                    { 
                        return this.Row.Theme[this.Row.RowId + 1][this.ObjectId + 1];
                    }
                }
                return null;
            }
        }

        public IThemeObject LeftBot
        {
            get
            {
                if (this.Row.RowId < this.Row.Theme.Count - 1 || (this.Row.IsLongRow && this.Row.RowId + 1 < this.Row.Theme.Count - 1))
                {
                    if (this.Row.IsLongRow)
                    {
                        if (this.ObjectId == 0)
                        { 
                            return this.Row.Theme[this.Row.RowId + 2][this.ObjectId];
                        }
                        return this.Row.Theme[this.Row.RowId + 1][this.ObjectId - 1];
                    }
                    else
                    { 
                        return this.Row.Theme[this.Row.RowId + 1][this.ObjectId];
                    }
                }
                return null;
            }
        }

        public void SwapThreads()
        {
            IThread tempThread = this.botThread;
            this.botThread = this.topThread;
            this.topThread = tempThread;
        }

        public int CreatePathToColor(Color c)
        {
            // Get starting path
            int leftPath = 0;
            if (this.LeftTop != null)
            {
                leftPath = this.LeftTop.CreatePathToColor(c);
            }
            int rightPath = 0;
            if (this.RightTop != null)
            {
                rightPath = this.RightTop.CreatePathToColor(c);
            }

            // Calculate current path
            if (leftPath > rightPath)
            {
                if (!this.RightTop.BotThread.ThreadColor.HasValue || !this.RightTop.TopThread.ThreadColor.HasValue)
                {
                    return ++rightPath;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                return ++leftPath;
            }

            return 0;
        }

        #endregion

        #region Explicit IThemeObject Memebers

        IThread IThemeObject.TopThread
        {
            get 
            {
                return this.topThread;
            }
        }

        IThread IThemeObject.BotThread
        {
            get 
            {
                return this.botThread;
            }
        }

        #endregion

    }
}
